package com.example.main.login

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_base.mvvm.BaseActivity
import com.example.main.R
import com.example.main.databinding.ActivityForgetPwactivityBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/login/ForgetPWActivity")
class ForgetPWActivity : BaseActivity<ActivityForgetPwactivityBinding,LoginViewModel>() {

    private var generatedCode: String? = null
    private var countDownTimer: android.os.CountDownTimer? = null
    private var isCountingDown = false

    override fun initView() {
        // 获取验证码
        binding.btnGetCod.setOnClickListener {
            if (isCountingDown) {
                android.widget.Toast.makeText(this, "倒计时结束后方可再次点击", android.widget.Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            generatedCode = (100000..999999).random().toString()
            android.widget.Toast.makeText(this, generatedCode, android.widget.Toast.LENGTH_SHORT).show()
            // 启动倒计时
            isCountingDown = true
            binding.btnGetCod.isEnabled = false
            countDownTimer = object : android.os.CountDownTimer(60000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    binding.btnGetCod.text = "${millisUntilFinished / 1000}s"
                }
                override fun onFinish() {
                    isCountingDown = false
                    binding.btnGetCod.isEnabled = true
                    binding.btnGetCod.text = "获取验证码"
                }
            }.start()
        }
        // 重置密码
        binding.btnResetPw.setOnClickListener {
            val inputCode = binding.etCode.text.toString()
            val phone = binding.etPhone.text.toString()
            val newPw = binding.etNewPw.text.toString()
            val confirmPw = binding.etConfirmPw.text.toString()
            if (phone.isEmpty() || inputCode.isEmpty() || newPw.isEmpty() || confirmPw.isEmpty()) {
                android.widget.Toast.makeText(this, "请填写完整信息", android.widget.Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (inputCode != generatedCode) {
                android.widget.Toast.makeText(this, "验证码错误", android.widget.Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (newPw != confirmPw) {
                android.widget.Toast.makeText(this, "两次密码不一致", android.widget.Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            vm.forgetPW(binding.etPhone.text.toString().toInt(), newPw)
            // 验证通过，返回登录页面

        }
        // 返回登录
        binding.tvBackLogin.setOnClickListener {
            finish()
        }
    }

    override fun initData() {
        vm.ForgetSuccess.observe(this){
            android.widget.Toast.makeText(this, "重置成功，返回登录", android.widget.Toast.LENGTH_SHORT).show()
            finish()
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        countDownTimer?.cancel()
    }
}